package com.wltjack.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wltjack.springboot.common.ResponseCode;
import com.wltjack.springboot.dto.UserDto;
import com.wltjack.springboot.mapper.UserMapper;
import com.wltjack.springboot.service.UserService;
import com.wltjack.springboot.vo.UserVO;
import com.wltjack.springboot.vo.common.PageDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private UserMapper userMapper;

	@Override
//	@Cacheable(value= {"AllUsersCache"},key="'crm.user.getAllUsers'")
	// value属性上用#号隔开，第一个是原始的缓存容器名称，第二个是缓存的有效时间，第三个是缓存的自动刷新时间，单位都是秒。
	public PageDto getAllUsers(UserDto userDto) {
		PageDto pageDto = new PageDto();
		PageHelper.startPage(userDto.getPageIndex(), userDto.getPageSize());
		List<UserVO> dataList = userMapper.selectUserList(userDto);
		pageDto.setDataList(dataList);
		PageInfo<?> pageInfo = new PageInfo<>(dataList);
		pageDto.setDataCount(pageInfo.getTotal());
		pageDto.setPageIndex(userDto.getPageIndex());
		pageDto.setPageSize(userDto.getPageSize());
		return pageDto;
	}

	@Override
//	@CacheEvict(value= {"AllUsersCache"},key="'crm.user.getAllUsers'")
	public String deleteUserById(Integer id) {
		log.info("delete user, id = {}",id);
		userMapper.deleteUserById(id);
		return ResponseCode.DATA_OK;
	}

	@Override
//	@CacheEvict(value= {"AllUsersCache"},key="'crm.user.getAllUsers'")
	public String addUser(UserVO userVO) {
		userMapper.insertUser(userVO);
		return ResponseCode.DATA_OK;
	}

	@Override
//	@CacheEvict(value= {"AllUsersCache"},key="'crm.user.getAllUsers'")
	public String updateUser(UserVO userVO) {
		userMapper.updateUser(userVO);
		return ResponseCode.DATA_OK;
	}

	@Override
//	@Cacheable(value= {"UserDetailCache"},key="'getUserDetailById_'+#id")
	public UserVO getUserDetailById(Integer id) {
		return userMapper.getUserDetailById(id);
	}

}
